//
//  Expression2VC.swift
//  Game
//
//  Created by woniu on 2025/8/22.
//

import Foundation
import UIKit
import SnapKit

class Expression2VC: BaseVC {
 
    public var gameModel:GameModel!
    
    private var bigImageView:UIImageView!
    private var scrollView:BottomScrollView!
    private var items:[ScrollItem] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .black
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
//        let woreList:[String] = ["blue","green","orange","pink","purple","red","yellow","blue","green","orange","pink","blue","green","orange","pink","purple"]
        for (index,wordItem) in gameModel.words.enumerated(){
            let item = ScrollItem()
            item.word = wordItem.word
            item.image = wordItem.image
            item.imgName = wordItem.imgName
            item.didModifyImg = wordItem.didModifyImg
            item.index = index
            self.items.append(item)
        }
        setupSubviews()
       
    }
    
    private func setupSubviews(){
        //一定要最先初始化
        bigImageView = UIImageView()
        bigImageView.contentMode = .scaleAspectFit
        bigImageView.circular5()
        self.view.addSubview(bigImageView)
        
        scrollView = BottomScrollView(frame: .zero)
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-15)
            make.width.equalTo(scrollView.getTotalW())
            make.centerX.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(0.4)
        }
        scrollView.delegate = self
        scrollView.update(by:items,defaultSelectedIndex: 2)
        
        //
        bigImageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.width.equalTo(bigImageView.snp.height)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(scrollView.snp.top)
        }
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeRight
    }
    
    override var shouldAutorotate: Bool {
        return true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIViewController.attemptRotationToDeviceOrientation()
    }
}

extension Expression2VC:BottomScrollViewDelegate{
    func onSelected(item: ScrollItem, view: BottomScrollView) {
        if let icon = item.image{
            self.bigImageView?.image = icon
        }else if !item.imgName.isEmpty{
            if let icon = GameManager.share.getImageBy(item.imgName){
                self.bigImageView.image = icon
                item.image = icon
            }
        }
        print("选中了模型:\(item.index)")
    }
}
